// pages/serverDetail/serverDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiveUserId: 0,
    list: [],
    show:true,
    publishId:0,
    address: [],
    img: "../../icons/header.png",
    name: "王师傅",
    praiseCount: 1332,
    attentionCount: '98%',
    complaintCount: 0,
    signature: "我们专注于监控的安装有",
    attention: "关注",
    phoneShow:false,
    address: [{
        address: "南昌县"
      },
      {
        address: "新建县"
      }
    ],
    introduce: [{
        introduce: "视频监控"
      },
      {
        introduce: "门禁"
      }
    ],
    introduceDetail: "专注弱电服务三十年，完成数百大小企业的项目，具有整套的服务流程",
    promise: [{
        promise: "1.三公里内不收服务费"
      },
      {
        promise: "2.接单1天内处理"
      },
      {
        promise: "3.空跑费50元/次"
      },
      {
        promise: "4.安装后一个月内免检修费"
      }
    ]

  },
  attention: function() {
    var that = this;
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId")==undefined){
      wx.switchTab({
        url: '../orderUser/orderUser',
      })
      setTimeout(() => {
        wx.showModal({
          title: '提示',
          content: '请先完成身份认证',
        })
      }, 500)      
      return false
    }
    if (this.data.attention == "关注") {
      wx.showModal({
        title: '提示',
        content: '是否关注该师傅',
        success: function(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/user/star',
              data: {
                userId: wx.getStorageSync("userId"),
                receiveUserId: that.data.receiveUserId,
                star: true

              },
              method: "post",
              success: function(res) {
                console.log(res);
                that.setData({
                  attention: "已关注"
                })
              }
            })

          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '确定要取关该师傅',
        success: function(res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/user/star',
              data: {
                userId: wx.getStorageSync("userId"),
                receiveUserId: that.data.receiveUserId,
                star: false

              },
              method: "post",
              success: function(res) {
                console.log(res);
                that.setData({
                  attention: "关注"
                })
              }



            })
          }
        }
      })
    }


  },
  // 确认师傅
  confirm: function (e) {
    var that = this;
  
    wx.showModal({
      title: '提示',
      content: "确认由" + that.data.list[0].name + "师傅接单",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
            data: {
              status: 5,
              publishId: that.data.publishId,
              orderId: 252,
              receiveUserId: that.data.receiveUserId
            },
            method: 'post',
            success: function (res) {
              console.log(res);
              wx.redirectTo({
                url: '../servering/servering?userId=' + res.data.data + "&receiveUserId=" + that.data.receiveUserId,
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })
  },
  // 拒绝师傅抢单
  refuse: function (e) {
    var that = this;
  
    wx.showModal({
      title: '提示',
      content: "确认要拒绝" + that.data.list[0].name + "师傅",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/serviceOrder/grabRecord/update',
            data: {
              receiveUserId: that.data.receiveUserId,
              publishId: that.data.publishId
            },
            method: 'post',
            success: function (res) {
              console.log(res);
              wx.navigateBack({
                
              })
              // that.masterList();
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })


  },
  // 联系师傅
  call(e){
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    var that = this;
    this.setData({
      receiveUserId: options.id,
    })
    console.log(options.fromOrder)
    if(options.fromOrder=="true"){
      that.setData({
        phoneShow:true
      })
      console.log(111)
    }else{

    }
    console.log(options.publishId);
    if(options.publishId!=undefined){
      that.setData({
        publishId:options.publishId,
        show:false
      })
      
    }else{
      that.setData({
        show:true
      })
    }

    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findByReceiveUserId',
      method: "post",
      data: {
        receiveUserId: that.data.receiveUserId
      },
      success: function(res) {
        console.log(res);
        var add = [];
        add.push(res.data.data.area.split(","));
        // console.log(add);
      console.log(res.data.data.area);
        var introduce = [];
        if (res.data.data.introduceList!=""){
          introduce = res.data.data.introduceList;
          // introduce = introduce[0].split(",")
        }else{
          introduce=""
        }
         
        console.log(introduce);
        that.setData({
          list: [res.data.data],
          address: add[0],
          introduce: introduce
        })
        console.log(that.data.list);
      }
    });
    wx.request({
      url: 'https://www.xiaozheng8.com/user/isStar',
      data: {
        userId:wx.getStorageSync("userId"),
        receiveUserId:that.data.receiveUserId
      },
      method: 'post',
  
      success: function(res) {
        console.log(res);
        that.setData({
          star:res.data.data
        });
        if(that.data.star==true){
          that.setData({
            attention: "已关注"
          })
        }else{
          that.setData({
            attention: "关注"
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  complain: function() {
    var that = this;
    wx.navigateTo({
      url: '../complain/complain?receiveUserId=' + that.data.receiveUserId,
    })
  }
})